var fs = require('fs');
var model = require('../../../model/model');
var logger = require('pomelo-logger').getLogger('playerRemote',__filename);
module.exports = function(app)
{
    return new playerRemote(app);
};

var playerRemote = function(app)
{
    this.app = app;
    this.channelService = app.get('channelService');
};

var handler = playerRemote.prototype;

handler.login = function(playerId,serverId,channelName,cb)
{
    var self = this;
    var channel = this.channelService.createChannel(channelName);
    if( !!channel )
    {
        channel.add(playerId,serverId);
        model.doLogin(playerId);
    }
    model.getNews(function(err,res)
    {
        for( var i in res )
        {
            var msg = res[i];
            var user = {'uid':playerId,'sid':serverId};
            self.channelService.pushMessageByUids('onBroadcast',msg,[user]);
        }
    })
    cb();
};

handler.kick = function(uid, sid, name, cb)
{
    var channel = this.channelService.getChannel(name, false);
    if( !!channel )
    {
        channel.leave(uid, sid);
        console.info('player@%d leave the game',uid);
    }
    cb();
};